package service;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class TerminplanerConnectionPool {

  private DataSource ds;
  private static final TerminplanerConnectionPool instance = new TerminplanerConnectionPool();

  public static TerminplanerConnectionPool getInstanz() {
    return instance;
  }

  private TerminplanerConnectionPool() {
    try {
      initDataSource();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  private void initDataSource() throws Exception {
    Context initCtx;
    initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    ds = (DataSource) envCtx.lookup("jdbc/mysql");
    
  }
  
  
  public Connection getConnection() throws Exception {
    if(ds == null) {
    }
    Connection conn = ds.getConnection();
    return conn;
  }
  
  public void saveClose(Connection con) {
    try {
      if (con != null && !con.isClosed()) {
        con.close();
      }
    }
    catch (SQLException e) {
      // left blank intentionally
    }
  }
  
}
